package ${package}.service.impl;

import ${package}.service.${model}Service;
import ${package}.dao.${model}Dao;
import ${package}.dto.${model}Dto;
import ${package}.model.${model};
import ${package}.enums.ResultEnum;
import ${package}.exception.BusinessException;
import ${package}.util.BeanUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * Created by util on ${now?date}.
 */
@Service
public class ${model}ServiceImpl implements ${model}Service {

    @Autowired
    private ${model}Dao ${model?uncap_first}Dao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public boolean insert${model}(${model}Dto ${model?uncap_first}Dto){
    	${model} ${model?uncap_first} = new ${model}();
    	BeanUtil.copyProperties(${model?uncap_first}Dto, ${model?uncap_first});
        int num = ${model?uncap_first}Dao.insert${model}(${model?uncap_first});
        success(num);
        return true;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public boolean delete${model}(${model}Dto ${model?uncap_first}Dto){
    	${model} ${model?uncap_first} = new ${model}();
    	BeanUtil.copyProperties(${model?uncap_first}Dto, ${model?uncap_first});
        int num = ${model?uncap_first}Dao.delete${model}(${model?uncap_first});
        success(num);
        return true;
    }

	private void success(int num) {
        if (num != 1) {
            throw new BusinessException(ResultEnum.fail);
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public boolean update${model}(${model}Dto ${model?uncap_first}Dto){
    	${model} ${model?uncap_first} = new ${model}();
    	BeanUtil.copyProperties(${model?uncap_first}Dto, ${model?uncap_first});
        int num = ${model?uncap_first}Dao.update${model}(${model?uncap_first});
        success(num);
        return true;
    }
    
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public ${model} select${model}ById(${model}Dto ${model?uncap_first}Dto){
        ${model} ${model?uncap_first} = ${model?uncap_first}Dao.select${model}ById(${model?uncap_first}Dto.getId());
        if (${model?uncap_first} == null) {
        	return new ${model}();
        }
        return ${model?uncap_first};
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public List<${model}> select${model}List(${model}Dto ${model?uncap_first}Dto){
    	${model} ${model?uncap_first} = new ${model}();
    	BeanUtil.copyProperties(${model?uncap_first}Dto, ${model?uncap_first});
        return ${model?uncap_first}Dao.select${model}List(${model?uncap_first});
    }
    
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public PageInfo select${model}Page(${model}Dto ${model?uncap_first}Dto){
    	${model} ${model?uncap_first} = new ${model}();
    	BeanUtil.copyProperties(${model?uncap_first}Dto, ${model?uncap_first});
        return ${model?uncap_first}Dao.select${model}Page(${model?uncap_first}, ${model?uncap_first}Dto.getPageNum(), ${model?uncap_first}Dto.getPageSize());
    }

}